#ifndef MOVABLEWIDGET_HPP
#define MOVABLEWIDGET_HPP

/********************************************************************
	created:	2011/01/17
	filename: 	E:\C++\Red\src\Graphics\Gui\MovableWidget.hpp
	author:		Red
	
	purpose:	Movable template to be inherited by widgets that need
				to be freely movable with mouse
*********************************************************************/

#include "MouseListener.hpp"
#include "Widget.hpp"

namespace red {
	namespace ui {
		/// Mouse Movable Widget
		class REDAPI MovableWidget : public Widget, public MouseListener {
		public:
			MovableWidget() : Widget(),
							  MouseListener(),
							  mIsMoving(false){
				SetAlignment(Absolute);
			}

		protected:
			// MouseListener function overload
				/// Mouse pressed : Moving can begin. Handle point saved
				virtual void OnMouseButtonPressed(const sf::Event::MouseButtonEvent &pEvent){
					mIsMoving = true;
					mHandlePoint = Vector2F((f32)pEvent.X, (f32)pEvent.Y);
				}

				/// Mouse released : Moving stops
				virtual void OnMouseButtonReleased(const sf::Event::MouseButtonEvent &pEvent){
					mIsMoving = false;
				}

				/// Mouse moved : Move the widget if IsMoving is true
				virtual void OnMouseMoved(const sf::Event::MouseMoveEvent &pEvent){
					if(mIsMoving){
						SetPosition(Vector2F(GetPosition().x + (f32)pEvent.X - mHandlePoint.x, 
											 GetPosition().y + (f32)pEvent.Y - mHandlePoint.y));
						mHandlePoint = Vector2F((f32)pEvent.X, (f32)pEvent.Y);
					}
				}


			bool		mIsMoving;		///< True if the widget is in motion
			Vector2F	mHandlePoint;	///< Mouse handle point on the widget
		};
	}
}
#endif // MOVABLEWIDGET_HPP